package com.smh.a01queryDemo01;

public class TileSearchDemo4 {
    public static void main(String[] args) {

        int[] arr = {27,22,30,40,36,
                    13,19,16,20,
                    7,10,
                    43,50,48};
        Block2 b1 = new Block2(40,27,0,4);
        Block2 b2 = new Block2(20,13,5,8);
        Block2 b3 = new Block2(10,7,9,10);
        Block2 b4 = new Block2(50,43,11,13);

        Block2[] blocks = {b1,b2,b3,b4};

        System.out.println(tileSearch(blocks,arr,40));

    }

    public static int tileSearch(Block2[] blocks,int[] arr,int num){

        int index=findIndexBlock(blocks,num);
        if (index<0){
            return -1;
        }
        int min = blocks[index].getStartIndex();
        int max = blocks[index].getEndIndex();
        for (int i = min; i < max; i++) {
            if (arr[i]==num){
                return i;
            }
        }
        return -1;
    }
    public static int findIndexBlock(Block2[] blocks,int num){
        for (int i = 0; i < blocks.length; i++) {
            if (blocks[i].getMax()>=num&&blocks[i].getMin()<=num){
                return i;
            }
        }
        return -1;

    }
}

class Block2{
    private int max;//最大值
    private int min;//最小值
    private int startIndex;//起始索引
    private int endIndex;//结束索引


    public Block2() {
    }

    public Block2(int max, int min, int startIndex, int endIndex) {
        this.max = max;
        this.min = min;
        this.startIndex = startIndex;
        this.endIndex = endIndex;
    }

    /**
     * 获取
     * @return max
     */
    public int getMax() {
        return max;
    }

    /**
     * 设置
     * @param max
     */
    public void setMax(int max) {
        this.max = max;
    }

    /**
     * 获取
     * @return min
     */
    public int getMin() {
        return min;
    }

    /**
     * 设置
     * @param min
     */
    public void setMin(int min) {
        this.min = min;
    }

    /**
     * 获取
     * @return startIndex
     */
    public int getStartIndex() {
        return startIndex;
    }

    /**
     * 设置
     * @param startIndex
     */
    public void setStartIndex(int startIndex) {
        this.startIndex = startIndex;
    }

    /**
     * 获取
     * @return endIndex
     */
    public int getEndIndex() {
        return endIndex;
    }

    /**
     * 设置
     * @param endIndex
     */
    public void setEndIndex(int endIndex) {
        this.endIndex = endIndex;
    }

    public String toString() {
        return "Block2{max = " + max + ", min = " + min + ", startIndex = " + startIndex + ", endIndex = " + endIndex + "}";
    }
}

